import {Component, ElementRef, HostListener, OnInit, ViewChild, AfterViewInit} from '@angular/core';
import {FetchService} from "../../../core/net/fetch.service";
import {NzInputDirective, NzMessageService} from "ng-zorro-antd";
import {differenceInCalendarDays} from 'date-fns';
import * as _ from 'lodash';
import {UtilService} from "../../../core/util/util.service";
import * as moment from 'moment';
import { Router } from '@angular/router';
import { DispatchPlanService } from '../dispatch-plan.service';

const initialAvg = {
    id: 'avg',
    m: '平均流量',
    val1: null,
    val2: null,
    val3: '-',
    val4: null,
    val5: '-',
    val6: null,
    val7: null,
    val8: null,
    val9: '-',
    val10: null,
    val11: null,
    val12: null
};
const initialTotal = {
    id: 'total',
    m: '合计水量',
    val1: null,
    val2: '-',
    val3: null,
    val4: '-',
    val5: null,
    val6: null,
    val7: '-',
    val8: '-',
    val9: null,
    val10: null,
    val11: null,
    val12: null
};
const intlTableData = [
    {
        id: 1,
        m: "11",
        val1: null,
        val2: null,
        val3: null,
        val4: null,
        val5: null,
        val6: null,
        val7: null,
        val8: null,
        val9: null,
        va10: null,
        va11: null,
        va12: null
    },
    {
        id: 2,
        m: "12",
        val1: null,
        val2: null,
        val3: null,
        val4: null,
        val5: null,
        val6: null,
        val7: null,
        val8: null,
        val9: null,
        va10: null,
        va11: null,
        va12: null
    },
    {
        id: 3,
        m: "1",
        val1: null,
        val2: null,
        val3: null,
        val4: null,
        val5: null,
        val6: null,
        val7: null,
        val8: null,
        val9: null,
        va10: null,
        va11: null,
        va12: null
    },
    {
        id: 4,
        m: "2",
        val1: null,
        val2: null,
        val3: null,
        val4: null,
        val5: null,
        val6: null,
        val7: null,
        val8: null,
        val9: null,
        va10: null,
        va11: null,
        va12: null
    },
    {
        id: 5,
        m: "3",
        val1: null,
        val2: null,
        val3: null,
        val4: null,
        val5: null,
        val6: null,
        val7: null,
        val8: null,
        val9: null,
        va10: null,
        va11: null,
        va12: null
    },
    {
        id: 6,
        m: "4",
        val1: null,
        val2: null,
        val3: null,
        val4: null,
        val5: null,
        val6: null,
        val7: null,
        val8: null,
        val9: null,
        va10: null,
        va11: null,
        va12: null
    },
    {
        id: 7,
        m: "5",
        val1: null,
        val2: null,
        val3: null,
        val4: null,
        val5: null,
        val6: null,
        val7: null,
        val8: null,
        val9: null,
        va10: null,
        va11: null,
        va12: null
    },
    {
        id: 8,
        m: "6",
        val1: null,
        val2: null,
        val3: null,
        val4: null,
        val5: null,
        val6: null,
        val7: null,
        val8: null,
        val9: null,
        va10: null,
        va11: null,
        va12: null
    },
    {
        id: 'avg',
        m: '平均流量',
        val1: null,
        val2: null,
        val3: '-',
        val4: null,
        val5: '-',
        val6: null,
        val7: null,
        val8: null,
        val9: '-',
        val10: null,
        val11: null,
        val12: null
    },
    {
        id: 'total',
        m: '合计水量',
        val1: null,
        val2: '-',
        val3: null,
        val4: '-',
        val5: null,
        val6: null,
        val7: '-',
        val8: '-',
        val9: null,
        val10: null,
        val11: null,
        val12: null
    }
];
const intlMonthTableData = [
    {
        id: 1,
        name: "流量",
        m: null,
        val1: null,
        val2: null,
        val3: null,
        val4: null,
        val5: null,
        val6: null,
        val7: null,
        val8: null,
        val9: null,
        va10: null,
        va11: null,
        va12: null
    },
    {
        id: 2,
        name: "水量",
        m: null,
        val1: null,
        val2: null,
        val3: null,
        val4: null,
        val5: null,
        val6: null,
        val7: null,
        val8: null,
        val9: null,
        va10: null,
        va11: null,
        va12: null
    }
];
const intlTenTableData = [
    {
        id: 1,
        name: "流量",
        m: null,
        val1: null,
        val2: null,
        val3: null,
        val4: null,
        val5: null,
        val6: null,
        val7: null,
        val8: null,
        val9: null
    },
    {
        id: 2,
        name: "水量",
        m: null,
        val1: null,
        val2: null,
        val3: null,
        val4: null,
        val5: null,
        val6: null,
        val7: null,
        val8: null,
        val9: null
    }
];
const XunHash = {
    'F': '上',
    'S': '中',
    'T': '下'
};
const TableIntlTempHash = {
    '0': intlTableData,
    '1': intlMonthTableData,
    '2': intlTenTableData
};
const monthOptions = [
    {value: "", label: "全部"},
    {value: "4", label: "4月"},
    {value: "5", label: "5月"},
    {value: "6", label: "6月"},
    {value: "7", label: "7月"},
    {value: "8", label: "8月"},
    {value: "9", label: "9月"},
    {value: "d", label: "冬灌"},
];

@Component({
    selector: 'app-hwh-dispatch-plan',
    templateUrl: './hwh-dispatch-plan.component.html',
    styleUrls: ['./hwh-dispatch-plan.component.scss']
})
export class HwhDispatchPlanComponent implements OnInit, AfterViewInit {
    tableData = [];
    year = "";    // 默认查后一年年
    month = "";
    monthOptions = monthOptions;
    tableHeight = "400px";
    scrollConfig =  {
        y: '400px'
    };
    pageNum = 1;
    pageSize = 10;
    total: number;
    loading = true;
    @ViewChild('wrap') elementView: ElementRef;
    constructor(
        private fetchService: FetchService,
        private util: UtilService,
        private router: Router,
        private dispatchService: DispatchPlanService,
    ) {
    }

    ngOnInit() {
        this.getGridData();
    }
    ngAfterViewInit() {
        this.scrollConfig.y = (this.elementView.nativeElement.offsetHeight - 160) + 'px';
    }
    getGridData(reset: boolean = false): void {
        if (reset) {
            this.pageNum = 1;
        }
        const params = {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            reportId: 2,
            cycle: this.year ? moment(this.year).year() : "" ,
            m: this.month || "",
            orderByField: "cycle desc ",
            orderBy: ", cast(m as UNSIGNED INTEGER)  desc,data_type  desc",
        };
        this.fetchService.post<any>('plan/wrsSchedulePlan/list', params).subscribe(res => {
            this.loading = false;
            if (res) {
                this.total = res['total'];
                this.tableData = res['list'];
            } else {
                this.total = 0;
                this.tableData = [];
            }
        });
    }

    disabledDate = (current: Date): boolean => {
        // 不能选明年之后的日期
        return differenceInCalendarDays(current, moment().add(1, 'y').valueOf()) > 0;
    }

    openDetailPage(type, y?, m?, dataType?, name?): void {
        this.router.navigate(['/dispatch-plan/hwh-dispatch-add'], { skipLocationChange: true, queryParams: {type: type, y: y, m: m, dataType: dataType, name: name}});
    }

    exportFile(data): void {
        const params = {
            reportId: '2',
            y: data.cycle,
            m: data.m,
            dataType: data.dataType,
        };
        this.dispatchService.exportExcel(params, data.name);
    }

    reset() {
        this.year = "";
        this.month = "";
        this.getGridData(true);
    }
}
